package com.apware.utils.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间解析
 * 
 * @author
 * 
 */
public class DateUtil {
	/**
	 * 时间格式数组
	 */
	private static String[] dateFormat = { "yyyy-MM-dd HH:mm:ss",
			"yyyy/MM/dd HH:mm:ss", "yyyy-MM-dd", "yyyy/MM/dd", "HH:mm:ss",
			"yyyyMMddHHmmss", "yyyyMMdd", "yyyy-MM-dd HH:mm", "yyyyMMddHHmm",
			"yyyy/MM/dd HH:mm", "yyyyMMddHHmmss", "yyyyMMddhhmmssSSS",
			"yyyyMMddHH", "MM-dd", "yyyy", "HH:mm", "yyyy-MM" };

	/**
	 * 
	 * 将字符串时间按格式转换为long
	 * 
	 * @param dateStr
	 *            字符串时�?
	 * @param index
	 *            时间格式数组下标
	 * @return Calendar
	 */
	public static long parseLongTime(String dateStr, int index) {
		if (dateStr == null || dateStr.trim().length() == 0) {
			return 0;
		} else {
			Date result = parseDate(dateStr, index);
			Calendar cal = Calendar.getInstance();
			cal.setTime(result);
			long time = cal.getTimeInMillis();
			return time;
		}
	}

	/**
	 * 
	 * 将字符串时间按格式转换为Date
	 * 
	 * @param dateStr
	 *            字符串时�?
	 * @param index
	 *            时间格式数组下标
	 * @return Date
	 */
	public static Date parseDate(String dateStr, int index) {
		DateFormat df = null;
		df = new SimpleDateFormat(dateFormat[index]);
		try {
			return df.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取�?整除的时间戳
	 * 
	 * @return
	 */
	public static String getTimeStamp() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int num = minute % 5;
		if ((num) != 0) {
			minute = minute - num;
		}
		String timeStamp = year + getZero(month) + getZero(day) + getZero(hour)
				+ getZero(minute);
		return timeStamp;
	}

	/**
	 * 时间戳补�?
	 * 
	 * @param num
	 * @return
	 */
	private static String getZero(int num) {
		String str = String.valueOf(num);
		if (str.length() == 1) {
			str = "0" + str;
		}
		return str;
	}

	/**
	 * 
	 * 将毫秒数转换成字符串形式的时�?
	 * 
	 * @param millis
	 *            毫秒�?
	 * @param index
	 *            转出时间格式
	 * @return 字符串时�?
	 */
	public static String parseDateLong(long millis, int index) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(millis);
		String date = new SimpleDateFormat(dateFormat[index]).format(cal
				.getTime());
		return date;
	}

	/**
	 * 
	 * 获取当前时间�?
	 * 
	 * @return 时间�?
	 */
	public static String getSysTimeStamp() {
		SimpleDateFormat sdFormat = new SimpleDateFormat(dateFormat[8]);
		Calendar cal = Calendar.getInstance();
		String myTime = sdFormat.format(cal.getTime());
		return myTime;
	}

	/**
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static String getSysDateTime() {
		SimpleDateFormat sdFormat = new SimpleDateFormat(dateFormat[0]);
		Calendar cal = Calendar.getInstance();
		String myTime = sdFormat.format(cal.getTime());
		return myTime;
	}

	/**
	 * 获取当前日期
	 * 
	 * @param index
	 * @return
	 */
	public static String getDate(int index) {
		SimpleDateFormat sdFormat = new SimpleDateFormat(dateFormat[index]);
		Calendar cal = Calendar.getInstance();
		String myTime = sdFormat.format(cal.getTime());
		return myTime;
	}

	/**
	 * 获取格式化后的日�?
	 * 
	 * @return
	 */
	public static String getTomorrow() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);

		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);

		String timeStamp = year + getZero(month) + getZero(day);
		return timeStamp;
	}
	
	public static String getPreDay() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);

		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);

		String timeStamp = year +"-"+ getZero(month)+"-" + getZero(day);
		return timeStamp;
	}

	/**
	 * 格式化输出日期
	 * 
	 * @param date
	 *            日期
	 * @param format
	 *            格式
	 * @return 返回字符型日期
	 */
	public static String format(java.util.Date date, String format) {
		String result = "";
		try {
			if (date != null) {
				java.text.DateFormat df = new java.text.SimpleDateFormat(format);
				result = df.format(date);
			}
		} catch (Exception e) {
		}
		return result;
	}

	/**
	 * 获取当月天数
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getDays(int year, int month) {
		int days;
		int FebDay = 28;
		if (isLeap(year))
			FebDay = 29;
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			days = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			days = 30;
			break;
		case 2:
			days = FebDay;
			break;
		default:
			days = 0;
			break;
		}
		return days;
	}

	// 判断闰年
	public static boolean isLeap(int year) {
		if (((year % 100 == 0) && year % 400 == 0)
				|| ((year % 100 != 0) && year % 4 == 0))
			return true;
		else
			return false;
	}
	
	
	public static int getYear(String desdate){
		int year=0;
		try
		{
			DateFormat fa = new SimpleDateFormat("yyyy-MM-dd");
			Date date = fa.parse(desdate);			
			year = date.getYear();
		}catch(Exception ex){
			
		}
		
		return year;
		
		
	}
}
